/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modelo.Controler;

import Model.Beans.Student;
import Model.Servicios.StudentSrv;
import Model.Servicios.UserSrv;
import com.opensymphony.xwork2.ActionSupport;

/**
 *
 * @author Eduardo
 */
public class UserDetailsAction extends ActionSupport {

    private StudentSrv studentSrv = new StudentSrv();
    private String studentId;
    private Student student;

    public String show() {
        student = studentSrv.getStudentById(studentId);
        return "show";
    }
    
    public String update() {
        student = studentSrv.getStudentById(studentId);
        return "update";
    }
    
    public String complete() {
        Student studentUpdated = studentSrv.getStudentById(student.getUser().getIduser().toString());
        studentUpdated.setFirstName(student.getFirstName());
        studentUpdated.setLastName(student.getLastName());
        studentUpdated.setEnrollment(student.getEnrollment());
        studentUpdated.getUser().setUsername(student.getUser().getUsername());
        studentUpdated.getUser().setPassword(student.getUser().getPassword());
        studentSrv.updateStudent(studentUpdated);
        new UserSrv().updateUser(studentUpdated.getUser());
        addActionMessage("Los datos han sido actualizados!");
        return "complete";
    }

    @Override
    public void validate() {
        if (getStudent() != null) {
            if (getStudent().getFirstName().equals("")) {
                addFieldError("student.firstName", "Falta nombre!");
            }
            if (getStudent().getLastName().equals("")) {
                addFieldError("student.lastName", "Falta apellido!");
            }
            if (getStudent().getEnrollment().equals("")) {
                addFieldError("student.enrollment", "Falta matricula!");
            }
            if (getStudent().getUser().getUsername().equals("")) {
                addFieldError("student.user.username", "Falta usuario!");
            }
            if (getStudent().getUser().getPassword().equals("")) {
                addFieldError("student.user.password", "Falta password!");
            }
        }
    }

    public String getStudentId() {
        return studentId;
    }

    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }

    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }
}
